// 提醒消息
// noinspection JSUnusedLocalSymbols

import {ElementUtils} from "@/assets/js/ElementUtils";
import {getTimestamp} from "@ginstone/common-utils/dist/src/utils/DateUtils";
import {NukeApi} from "@ginstone/nga-api/dist/src/method/NukeApi";
import {NoticeEnableParam} from "@ginstone/nga-api/dist/src/params/NukeParams";

// 计算未读消息数量
const countUnread = (arr) => {
    return arr.filter(i => !i.hasOwnProperty("read")).length
}
// 合并两个提醒消息数组
const mergeNotices = (ov, nv) => {
    if (!nv) {
        return ov
    }
    return [...nv.reverse(), ...ov]
}

export default {
    namespaced: true,
    state: {
        // 回复提醒
        replyNotices: [],
        // 赞踩提醒
        recommendNotices: [],
        // 私信提醒
        msgNotices: [],
        // 上一次发送请求的时间戳
        lastRequest: 1,
        // 未读消息总数
        unreadCount: 0,
        // 消息总数
        totalCount: 0,
    },
    mutations: {
        method(state, payload) {

        },
        // 清空消息
        clear(state) {
            state.replyNotices = []
            state.recommendNotices = []
            state.msgNotices = []
            state.unreadCount = 0
            state.totalCount = 0
            state.lastRequest = 1
        },
        // 全部已读
        readAll(state) {
            state.replyNotices.forEach(i => i.read = true)
            state.recommendNotices.forEach(i => i.read = true)
            state.msgNotices.forEach(i => i.read = true)
            state.unreadCount = 0
        },
        // 设置已读
        read(state, obj) {
            if (!obj.hasOwnProperty("read")) {
                obj.read = true;
                state.unreadCount--
            }
        },
        // 计算未读消息总数
        countUnread(state, payload) {
            state.unreadCount = countUnread(state.replyNotices) + countUnread(state.recommendNotices) + countUnread(state.msgNotices)
        },
    },
    actions: {
        // 查询请求
        noticeList: ({dispatch, commit, state, rootGetters}) => {
            const client = rootGetters["Account/getClient"]();
            const now = getTimestamp()
            return NukeApi.noticeList(client, state.lastRequest).then(res => {
                if (!res) {
                    return undefined
                }
                // 合并数据
                state.replyNotices = mergeNotices(state.replyNotices, res.replyNotices)
                state.recommendNotices = mergeNotices(state.recommendNotices, res.recommendNotices)
                state.msgNotices = mergeNotices(state.msgNotices, res.msgNotices)

                console.log(res)

                // 计算未读消息总数
                commit("countUnread")
                // 更新时间戳
                state.lastRequest = now;
                // 更新消息总数
                state.totalCount = state.replyNotices.length + state.recommendNotices.length + state.msgNotices.length
                return res
            })
        },
        // 关闭 / 开启某一楼提醒消息
        noticeEnable: ({dispatch, commit, state, rootGetters}, {tid, pid, no_hint}) => {
            const client = rootGetters["Account/getClient"]();
            const params = new NoticeEnableParam(tid, pid, no_hint);
            if (no_hint === 1) {
                return ElementUtils.confirm("即将关闭对该回复的提醒消息, 包括: 回复、送礼、赞踩变化；你可以到主题中对该回复操作以重新开启提醒。", "关闭提醒")
                    .then(res => {
                        return NukeApi.noticeEnable(client, params).then(res => {
                            ElementUtils.notice({message: res})
                            return res;
                        })
                    }).catch(e => {
                        console.warn(e)
                    })
            }
            if (no_hint === 0) {
                return NukeApi.noticeEnable(client, params).then(res => {
                    ElementUtils.notice({message: res})
                    return res;
                })
            }
        },
        // 清空提醒消息
        noticeClear: ({dispatch, commit, state, rootGetters}, payload) => {
            const client = rootGetters["Account/getClient"]();
            return ElementUtils.confirm("本操作不可撤销", "清理提醒消息").then(res => {
                return NukeApi.noticeClear(client).then(res => {
                    ElementUtils.notice({message: res})
                    commit("clear")
                    return res
                }).catch(ElementUtils.exceptionHandler)
            }).catch(e => {
                console.warn(e)
            })
        },

        method: ({dispatch, commit, state, rootGetters}, payload) => {

        },

    },
    getters: {},
}